# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http.response import HttpResponse
from django.shortcuts import render
from gongzhong import settings
from mygongzhong.models import WxUsers, WxEvents, token
import requests
import hashlib
import json
from lxml import etree
from mygongzhong.utils import *



def test(request):
    title = 'test1'
    thumb_media_id = 'Rslf0AOAU7nSva5eMHp5JNGlsSPEYAKEHVNE4J8Jb8w'
    author = 'ww'
    digest = '这是一个测试永久素材'
    show_cover_pic=1
    content='这是一个测试永久素材,这是一个测试永久素材这是一个测试永久素材这是一个测试永久素材这是一个测试永久素材这是一个测试永久素材这是一个测试永久素材'
    content_source_url='http://www.baidu.com'
    cs = CreateSource(title, thumb_media_id, author, digest, show_cover_pic, content, content_source_url)
    text = cs.s_lang_news()
    return HttpResponse(text)


def wx(request):
    if request.method == 'GET':
        data = request.GET
        echostr = data["echostr"]
        return HttpResponse(echostr)
    else:
        data = request.body
        xml = etree.fromstring(data)
        MsgType = xml.find("MsgType").text
        Event = xml.find("Event").text
        CreateTime = xml.find("CreateTime").text
        FromUserName = xml.find("FromUserName").text
        ToUserName = xml.find("ToUserName").text
        EventKey = xml.find("EventKey").text
        WxEvents.objects.create(MsgType=MsgType, Event=Event, CreateTime=CreateTime, FromUserName=FromUserName,
                                ToUserName=ToUserName)
        c = Createmsg(FromUserName, ToUserName)
        if MsgType == "event":
            if Event == "subscribe":
                user = getuserinfo(FromUserName)
                WxUsers.objects.create(province=user["province"], city=user["city"],
                                       subscribe_time=user["subscribe_time"], headimgurl=user["headimgurl"],
                                       language=user["language"], openid=user["openid"], country=user["country"],
                                       tagid_list=user["tagid_list"], remark=user["remark"], sex=user["sex"],
                                       subscribe=user["subscribe"], nickname=user["nickname"], groupid=user["groupid"])
                msg = "欢迎关注"
                text = c.t_text(msg)
                return HttpResponse(text)
            elif Event == "unsubscribe":
                WxUsers.objects.filter(openid=FromUserName).delete()
                return HttpResponse("success")
            elif Event == "CLICK":
                if EventKey == "V1001_KF_DETAIL":
                    title = "官方声明"
                    description = "官方声明官方声明官方声明官方声明官方声明官方声明"
                    picurl = "http://mmbiz.qpic.cn/mmbiz_jpg/ibIS4od5Fx1bYp0qjOgyibB8Ywc5iaqAyqgdSiaso1V6p252nGicCQRcibtibZUd3SJGJFkiaVfMw8R5rzPAL3YQiaR7XMA/0?wx_fmt=jpeg"
                    url = "http://mp.weixin.qq.com/s?__biz=MzU1MDI5NjMwMg==&mid=100000002&idx=1&sn=cfe864a680b2e28ad923a63d80d173c4&chksm=7ba38e834cd40795a1bf3c38f96e07feff116faea7236163bdeec7a42851537714e2bea8350c#rd"
                    text = c.t_news(title, description, picurl, url)
                elif EventKey == "V1001_KF_KF":
                    msg = "人工客服"
                    text = c.t_text(msg)
                else:
                    msg = "success"
                    text = c.t_text(msg)
                return HttpResponse(text)

            else:
                print FromUserName
            return HttpResponse("success")
        else:
            return HttpResponse("success")


def createmenu(request):
    mlist = {
        "button": [
            {
                "name": "客服中心",
                "sub_button": [
                    {
                        "type": "view",
                        "name": "红包验证",
                        "url": "http://www.baidu.com"
                    },
                    {
                        "type": "view",
                        "name": "更多游戏",
                        "url": "http://www.baidu.com"
                    },
                    {
                        "type": "click",
                        "name": "人工客服",
                        "key": "V1001_KF_KF"
                    },
                    {
                        "type": "click",
                        "name": "官方声明",
                        "key": "V1001_KF_DETAIL"
                    },
                    {
                        "type": "view",
                        "name": "棋牌资讯",
                        "url": "http://www.baidu.com"
                    }
                ]
            },
            {
                "type": "click",
                "name": "房卡购买",
                "key": "V1002_FK_BUY"
            },
            {
                "name": "代理加盟",
                "sub_button": [
                    {
                        "type": "click",
                        "name": "开通代理",
                        "key": "V1003_DL_ON"
                    },
                    {
                        "type": "view",
                        "name": "代理后台",
                        "url": "http://www.baidu.com"
                    },
                    {
                        "type": "click",
                        "name": "乱价处理",
                        "key": "V1003_DL_LJ"
                    },
                    {
                        "type": "click",
                        "name": "招商资讯",
                        "key": "V1003_DL_ZS"
                    }
                ]
            }
        ]}

    pdata = json.dumps(mlist, ensure_ascii=False).encode('utf8')
    print pdata
    token = gettoken()
    url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + token
    print url
    re = requests.post(url, data=pdata)
    return HttpResponse(re.text)


def userlist(request):
    token = gettoken()
    url = "https://api.weixin.qq.com/cgi-bin/user/get"
    args = {"access_token": token, "next_openid": ""}
    re = requests.get(url, params=args)
    return HttpResponse(re.text)
